<!--
 * @Author: 范玉超
 * @Date: 2023-06-06 10:20:26
 * @LastEditors: git config user.name && git config user.email
 * @LastEditTime: 2023-08-16 14:49:03
 * @FilePath: \erweima\src\views\indexsView.vue
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<template>
  <div class="index">
    <div class="index-box" id="index1">
      <div class="box" id="box">
        <div class="box-top">
          <div class="z_photo">
            <img :src="img" alt="" />
            <input
              class="fil"
              type="file"
              value=""
              accept="image/*"
              name="file"
              @change="handleFileChange"
            />
          </div>
          <div style="clear: both"></div>
          <div class="messagelist">
            <input v-if="!isSC" type="text" v-model="name" placeholder="姓名" />
            <div v-else style="width: 100%; text-align: center">
              {{ name }}
            </div>
          </div>
          <div class="messagelist1">
            <input
              v-if="!isSC"
              type="text"
              v-model="company"
              placeholder="公司名称"
            />
            <div v-else style="width: 100%; text-align: center">
              {{ company }}
            </div>
          </div>
          <div class="messagelist1">
            <input
              v-if="!isSC"
              type="text"
              v-model="zhiwu"
              placeholder="职位"
            />
            <div v-else style="width: 100%; text-align: center">
              {{ zhiwu }}
            </div>
          </div>
          <div class="messagelist1">
            <input
              v-if="!isSC"
              type="text"
              v-model="shehuizhiwu"
              placeholder="社会职务"
            />
            <div v-else style="width: 100%; text-align: center">
              {{ shehuizhiwu }}
            </div>
          </div>
          <div class="qrcode" v-if="show == false">
            <!-- <div class="qrcode" id="qrcode"></div> -->
            <div class="qrcode1">
              <img :src="res" alt="" />
            </div>
            <div class="qrcode-tip">（长按二维码保存联系方式到手机通讯录）</div>
          </div>
        </div>
        <div class="message" v-if="show">
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon1.png" alt="" />
            </div>
            <div class="txt">
              <input type="number" v-model="tel" placeholder="手机号" />
            </div>
          </div>
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon5.png" alt="" />
            </div>
            <div class="txt">
              <input type="text" v-model="desc" placeholder="微信号" />
            </div>
          </div>
          <!-- <div class="list">
          <div class="icon">
            <img src="@/assets/img/icon11.png" alt="" />
          </div>
          <div class="txt">{{ number }}</div>
        </div> -->
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon2.png" alt="" />
            </div>
            <div class="txt">
              <input type="text" v-model="email" placeholder="电子邮箱" />
            </div>
          </div>
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon3.png" alt="" />
            </div>
            <div class="txt">
              <input type="text" v-model="web" placeholder="网址" />
            </div>
          </div>
          <div class="list">
            <div class="icon">
              <img src="@/assets/img/icon4.png" alt="" />
            </div>
            <div class="txt" style="font-size: 0.75rem">
              <input type="text" v-model="address" placeholder="地址" />
            </div>
          </div>
        </div>
        <div class="message" v-else>
          <div class="list" v-if="tel">
            <div class="icon">
              <img src="@/assets/img/icon1.png" alt="" />
            </div>
            <div class="txt">
              <input type="number" v-model="tel" placeholder="手机号" />
            </div>
          </div>
          <div class="list" v-if="desc">
            <div class="icon">
              <img src="@/assets/img/icon5.png" alt="" />
            </div>
            <div class="txt">
              <input type="text" v-model="desc" placeholder="微信号" />
            </div>
          </div>
          <!-- <div class="list">
          <div class="icon">
            <img src="@/assets/img/icon11.png" alt="" />
          </div>
          <div class="txt">{{ number }}</div>
        </div> -->
          <div class="list" v-if="email">
            <div class="icon">
              <img src="@/assets/img/icon2.png" alt="" />
            </div>
            <div class="txt">
              <input type="text" v-model="email" placeholder="电子邮箱" />
            </div>
          </div>
          <div class="list" v-if="web">
            <div class="icon">
              <img src="@/assets/img/icon3.png" alt="" />
            </div>
            <div class="txt">
              <input type="text" v-model="web" placeholder="网址" />
            </div>
          </div>
          <div class="list" v-if="address">
            <div class="icon">
              <img src="@/assets/img/icon4.png" alt="" />
            </div>
            <div class="txt" style="font-size: 0.75rem">
              <input type="text" v-model="address" placeholder="地址" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="btn-box" v-if="show2">
      <div class="btn" @click="shengcheng">生成</div>
    </div>
    <div class="btn-box" v-if="show1">
      <div class="btn" @click="download">保存</div>
    </div>
  </div>
</template>
<script setup>
import html2canvas from "html2canvas";
import QRCode from "qrcode";
import { ref } from "vue";
const tel = ref(""); //电话
const name = ref(""); //姓名
const title = ref(""); //董事长
const company = ref(""); //公司名称
const zhiwu = ref(""); //职务
const shehuizhiwu = ref(""); //社会职务
// const number = ref("13735822550");
const email = ref(""); //邮箱
const web = ref(""); //网址
const address = ref(""); //地址
const desc = ref(""); //微信号
const show = ref("show"); //
const show1 = ref(false); //
const show2 = ref(true); //生成按钮
const res = ref("");
// const img = ref(require("../assets/img/tx.jpg"));
const img = ref(require("../assets/img/home_list_icon2.png"));

const isSC = ref(false);

// 上传头像
const handleFileChange = (el) => {
  let file = el.target.files[0];
  var reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = (e) => {
    img.value = e.target.result;
  };
};

// 转base64
const getBase64Image = (src) => {
  return new Promise((resolve) => {
    const img = new Image();
    img.crossOrigin = "";
    img.src = src;
    img.onload = function () {
      const canvas = document.createElement("canvas");
      canvas.width = img.width;
      canvas.height = img.height;
      const ctx = canvas.getContext("2d");
      ctx?.drawImage(img, 0, 0, img.width, img.height);
      const ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
      const dataURL = canvas.toDataURL("image/" + ext);
      resolve(dataURL);
    };
  });
};

// 生成二维码图片
const generateQR = async () => {
  // let avatar = await getBase64Image(img.value);
  try {
    var info =
      "BEGIN:VCARD\n" +
      "FN:" +
      name.value +
      "\n" +
      "ORG:" +
      company.value +
      "\n" +
      "TITLE:" +
      zhiwu.value +
      "\n" +
      "WORK:" +
      address.value +
      "\n" +
      "TEL:" +
      tel.value +
      "\n" +
      "EMAIL:" +
      email.value +
      "\n" +
      // "PHOTO;TYPE=JPEG;ENCODING=BASE64:" +
      // avatar +
      // "\n" +
      // "URL:" +
      // web.value +
      // "\n" +
      "NOTE:" +
      desc.value +
      "\n" +
      "END:VCARD";
    res.value = await QRCode.toDataURL(info);
    console.log(" res.value  ", res.value);
  } catch (err) {
    console.error(err);
  }
};

const download = () => {
  show1.value = false;
  setTimeout(() => {
    let canvasID = document.getElementById("index1"); // 获取要生成图片的页面元素
    html2canvas(canvasID, {
      scale: 6, //数值越大生成的图片越清晰
      backgroundColor: null, //解决生成的图片有白边
    }).then((canvas) => {
      let dataURL = canvas.toDataURL("image/png");
      // uploadFile(dataURL);
      // saveFile(dataURL, "图片名称");
      var url = canvas.toDataURL("image/png"); //得到图片的base64编码数据'
      var a = document.createElement("a"); // 生成一个a元素
      var event = new MouseEvent("click", {
        bubbles: true,
        cancelable: true,
        view: window,
      }); // 创建一个单击事件
      a.download = "photo"; // 设置图片名称
      a.href = url; // 将生成的URL设置为a.href属性
      a.dispatchEvent(event); // 触发a的单击事件
    });
  }, 500);
};
// 生成
const shengcheng = () => {
  show.value = false;
  show1.value = true;
  show2.value = false;
  isSC.value = true;
  generateQR();
};
</script>
<style lang="less" scoped>
.index {
  width: 100vw;
  min-height: 100vh;
  background-color: #0e87e2;
  .index-box {
    background-color: #0e87e2;
    padding: 1.25rem 1rem;
    .box {
      background-color: #f9f9f9;
      border-radius: 4px 4px 0 0;
      .box-top {
        text-align: center;
        border-radius: 4px 4px 0 0;
        padding: 0.3125rem 0rem 10px 0;
        background: #fff url(@/assets/img/title.png) no-repeat top center/100%
          40px;
        .z_photo {
          display: block;
          width: 100px;
          height: 100px;
          margin: 0 auto;
          border: 4px solid #fff;
          overflow: hidden;
          border-radius: 50%;
          overflow: hidden;
          position: relative;
          .fil {
            position: absolute;
            width: 100px;
            height: 90px;
            left: 50%;
            top: 0;
            margin-left: -3.125rem;
            opacity: 0;
          }
          img {
            width: 100%;
          }
        }
        .messagelist {
          max-width: 100%;
          // line-height: 1.8rem;
          // height: 1.8rem;
          font-size: 1.5rem;
          input,
          div {
            text-align: center;
            width: 100%;
            font-weight: bold;
            font-size: 1.5rem;
            color: #333;
            padding: 0;
            border: 0;
          }
          input::placeholder {
            font-size: 1.5rem;
            color: #333;
            font-weight: bold;
            padding: 0;
            border: 0;
          }
        }
        .messagelist1 {
          max-width: 100% !important;
          font-size: 1rem;
          color: #666;
          line-height: 1.5rem;
          letter-spacing: 0.0625rem;
          text-align: center;
          display: flex;
          align-items: center;
          input,
          div {
            text-align: center !important;
            font-size: 1rem;
            width: 100%;
            color: #666;
            letter-spacing: 0.0625rem;
            padding: 0;
            border: 0;
          }
          input::placeholder {
            text-align: center;
            font-size: 1rem;
            width: 100%;
            color: #999;
            letter-spacing: 0.0625rem;
            padding: 0;
            border: 0;
          }
        }
        .p1 {
          font-weight: bold;
          font-size: 1.5rem;
          color: #333;
          line-height: 1.5rem;
        }
        .p2 {
          font-size: 1rem;
          color: #666;
          line-height: 1.5rem;
          letter-spacing: 0.0625rem;
        }
      }
      // 二维码
      .qrcode1 img {
        display: block;
        width: 8.75rem;
        height: 8.75rem;
        margin: auto;
      }

      .qrcode-tip {
        text-align: center;
        color: #333;
        font-size: 0.8rem;
        color: #f96774;
      }
      //信息
      .message {
        width: 100%;
        padding: 0rem 0.8rem;
        box-sizing: border-box;
        .list {
          display: flex;
          align-items: center;
          padding: 0.5rem 0.3125rem;
          border-bottom: 0.0625rem solid #e4e4e4;
          .icon {
            width: 1.75rem;
            height: 1.75rem;
            img {
              width: 100%;
              font-size: 0;
              display: block;
            }
          }
          .txt {
            flex: 1;
            margin-left: 0.7125rem;
            font-size: 1rem;
            line-height: 1.75rem;
            color: #333;
            height: 1.75rem;
            input {
              width: 100%;
              height: 100%;
              line-height: 1.75rem;
            }
          }
        }
      }
    }
  }

  .btn-box {
    width: 100%;
    background: #0e87e2;
    padding: 0rem 1rem 1rem 1rem;
    .btn {
      width: 100%;
      height: 40px;
      background: #ddeefc;
      line-height: 40px;
      text-align: center;
      border-radius: 2px;
      font-size: 17px;
      color: #0b75d0;
      font-weight: bold;
      letter-spacing: 8px;
    }
  }
}
</style>
